package dao

import (
	"hft/Http/Models/request"

	"gorm.io/gorm"
)

type Advise struct{}

func (a *Advise) List(paginate *request.Advise, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(a.Time(paginate.CreateTime, db)).Scopes(a.Type(paginate.Type, db)).Scopes(a.City(paginate.City, db))
	}
}

func (a *Advise) Time(createTime []string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if len(createTime) != 2 {
			return db
		} else {
			return db.Where("create_time >= ? AND create_time <= ?", createTime[0], createTime[1])
		}
	}
}

func (a *Advise) City(city int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("city = ?", city)
	}
}

func (a *Advise) Type(adviseType int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("type = ?", adviseType)
	}
}

var AdviseDao = new(Advise)
